@echo off
setlocal enabledelayedexpansion

echo ================================================
echo Property Cleaning System - 32-bit Compatible Installation
echo ================================================
echo.
echo [WARNING] Running in 32-bit compatibility mode
echo.

:: Skip 64-bit check for compatibility
:: This script will install software that works on both 32-bit and 64-bit systems

echo [1/4] Installing Git (32-bit compatible)...
where git >nul 2>&1
if %errorlevel% neq 0 (
    powershell -Command "if (-not (Get-Command git -ErrorAction SilentlyContinue)) { Write-Host 'Installing Git...'; iwr -useb https://github.com/git-for-windows/git/releases/download/v2.42.0.windows.2/Git-2.42.0.2-32-bit.exe -OutFile git-installer.exe; Start-Process -Wait -FilePath git-installer.exe -Args '/VERYSILENT /NORESTART'; Remove-Item git-installer.exe }"
) else (
    echo Git already installed
)

echo [2/4] Installing Java 8+ (32-bit compatible)...
where java >nul 2>&1
if %errorlevel% neq 0 (
    powershell -Command "if (-not (Get-Command java -ErrorAction SilentlyContinue)) { Write-Host 'Installing Java...'; iwr -useb https://javadl.oracle.com/webapps/download/AutoDL?BundleId=248242_ce59cff5c23f4e2eaf4e778a117d4c5b -OutFile java-installer.exe; Start-Process -Wait -FilePath java-installer.exe -Args '/s'; Remove-Item java-installer.exe }"
) else (
    echo Java already installed
)

echo [3/4] Installing Node.js (32-bit compatible)...
where node >nul 2>&1
if %errorlevel% neq 0 (
    powershell -Command "if (-not (Get-Command node -ErrorAction SilentlyContinue)) { Write-Host 'Installing Node.js...'; iwr -useb https://nodejs.org/dist/v18.18.2/node-v18.18.2-x86.msi -OutFile node-installer.msi; Start-Process -Wait -FilePath msiexec.exe -Args '/i node-installer.msi /quiet'; Remove-Item node-installer.msi }"
) else (
    echo Node.js already installed
)

echo [4/4] Setting up manual deployment (Docker alternative)...
set "PROJECT_DIR=%USERPROFILE%\property-cleaning-system"
if not exist "%PROJECT_DIR%" (
    mkdir "%PROJECT_DIR%"
)

:: Create manual startup script (without Docker)
echo @echo off > "%PROJECT_DIR%\start-manual.bat"
echo echo Starting Property Cleaning System (Manual Mode)... >> "%PROJECT_DIR%\start-manual.bat"
echo cd /d "%~dp0" >> "%PROJECT_DIR%\start-manual.bat"
echo. >> "%PROJECT_DIR%\start-manual.bat"
echo echo [1/3] Starting MySQL database... >> "%PROJECT_DIR%\start-manual.bat"
echo start "" "C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe" --console >> "%PROJECT_DIR%\start-manual.bat"
echo timeout /t 5 /nobreak ^>nul >> "%PROJECT_DIR%\start-manual.bat"
echo. >> "%PROJECT_DIR%\start-manual.bat"
echo echo [2/3] Starting Java backend... >> "%PROJECT_DIR%\start-manual.bat"
echo cd backend >> "%PROJECT_DIR%\start-manual.bat"
echo start "" cmd /c "mvn spring-boot:run ^> backend.log 2^>^&1" >> "%PROJECT_DIR%\start-manual.bat"
echo cd .. >> "%PROJECT_DIR%\start-manual.bat"
echo timeout /t 10 /nobreak ^>nul >> "%PROJECT_DIR%\start-manual.bat"
echo. >> "%PROJECT_DIR%\start-manual.bat"
echo echo [3/3] Starting Node.js frontend... >> "%PROJECT_DIR%\start-manual.bat"
echo cd frontend >> "%PROJECT_DIR%\start-manual.bat"
echo start "" cmd /c "npm run dev ^> frontend.log 2^>^&1" >> "%PROJECT_DIR%\start-manual.bat"
echo cd .. >> "%PROJECT_DIR%\start-manual.bat"
echo timeout /t 5 /nobreak ^>nul >> "%PROJECT_DIR%\start-manual.bat"
echo. >> "%PROJECT_DIR%\start-manual.bat"
echo echo System started! >> "%PROJECT_DIR%\start-manual.bat"
echo echo Backend: http://localhost:8080 >> "%PROJECT_DIR%\start-manual.bat"
echo echo Frontend: http://localhost:3000 >> "%PROJECT_DIR%\start-manual.bat"
echo pause >> "%PROJECT_DIR%\start-manual.bat"

:: Create MySQL installation guide
echo MySQL Installation Guide: > "%PROJECT_DIR%\mysql-setup.txt"
echo. >> "%PROJECT_DIR%\mysql-setup.txt"
echo 1. Download MySQL 8.0 Community Server (32-bit) >> "%PROJECT_DIR%\mysql-setup.txt"
echo 2. Install with default settings >> "%PROJECT_DIR%\mysql-setup.txt"
echo 3. Set root password: root123 >> "%PROJECT_DIR%\mysql-setup.txt"
echo 4. Import database: mysql -u root -p < database/schema.sql >> "%PROJECT_DIR%\mysql-setup.txt"

:: Create desktop shortcuts
echo Set oWS = WScript.CreateObject("WScript.Shell") > "%PROJECT_DIR%\create-shortcuts.vbs"
echo sLinkFile = oWS.SpecialFolders("Desktop") ^& "\Property Cleaning System (32-bit).lnk" >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo oLink.TargetPath = "%PROJECT_DIR%\start-manual.bat" >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo oLink.WorkingDirectory = "%PROJECT_DIR%" >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo oLink.Description = "Property Cleaning System (32-bit compatible)" >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo oLink.Save >> "%PROJECT_DIR%\create-shortcuts.vbs"
echo Set oLink = Nothing >> "%PROJECT_DIR%\create-shortcuts.vbs"

"%PROJECT_DIR%\create-shortcuts.vbs"
del "%PROJECT_DIR%\create-shortcuts.vbs"

echo.
echo ================================================
echo 32-bit Compatible Installation Complete!
echo ================================================
echo.
echo [IMPORTANT] Manual steps required:
echo 1. Install MySQL 8.0 Community Server (32-bit)
echo 2. Run: mysql -u root -p < database/schema.sql
echo 3. Edit application.properties with MySQL credentials
echo 4. Run desktop shortcut: Property Cleaning System (32-bit)
echo.
echo Project directory: %PROJECT_DIR%
echo Setup guide: mysql-setup.txt
pause